<?php
	/** DB Model CLASS */
	$DBOBJ = new Db();

	/** 查詢DB 返回數組
	* @param String $tabname 	表名
	* @param Array	$param	 	參數
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @param String $debug		debug 0:不顯示sql語句 1:顯示sql語句
	* @return Array
	*/
	function db_query($tabname,$param=array(),$db='',$mode='read',$debug=0){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->query($tabname,$param,$dbconn,'',$debug);
	}

	/** 查詢DB 返回記錄數
	* @param String $tabname 	表名
	* @param String	$where	 	條件
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @param String $debug		debug 0:不顯示sql語句 1:顯示sql語句
	* @return Array
	*/
	function db_query_count($tabname,$where,$db='',$mode='read',$debug=0){
		global $DBOBJ;
		$param=array();
		$param['field'] = "count(*) as num";
		$param['where'] = $where;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		$data = $dbm->query($tabname,$param,$dbconn,'one',$debug);
		return intval($data['result']['num']);
	}

	/** 查詢DB 返回下標數組
	* @param String $tabname 	表名
	* @param Array	$param	 	參數
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @param String $debug		debug 0:不顯示sql語句 1:顯示sql語句	
	* @return Array
	*/
	function db_query_num($tabname,$param=array(),$db='',$mode='read',$debug=0){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->query($tabname,$param,$dbconn,'num',$debug);
	}

	/** 查詢DB 返回hash數組
	* @param String $tabname 	表名
	* @param Array	$param	 	參數
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @param String $debug		debug 0:不顯示sql語句 1:顯示sql語句	
	* @return Array
	*/
	function db_query_hash($tabname,$param=array(),$db='',$mode='read',$debug=0){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->query($tabname,$param,$dbconn,'hash',$debug);
	}

	/** 查詢DB 返回一條記錄數組
	* @param String $tabname 	表名
	* @param Array	$param	 	參數
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @param String $debug		debug 0:不顯示sql語句 1:顯示sql語句	
	* @return Array
	*/
	function db_query_one($tabname,$param=array(),$db='',$mode='read',$debug=0){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		$param['limit'] = 1;
		$param['pagesize'] = 0;
		return $dbm->query($tabname,$param,$dbconn,'one',$debug);
	}


	/** 插入記錄
	* @param String	$tabname	表名
	* @param Array	$data		key=>$value
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @return int insertID
	*/
	function db_add($tabname,$data=array(),$db='',$mode='write'){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->add($tabname,$data,$dbconn);
	}

	/** 更新記錄
	* @param String $tabname	表名
	* @param Array	$data		$key=>$value
	* @param String	$condition	條件
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	* @return int affected count
	*/    
	function db_update($tabname,$data=array(),$condition='',$db='',$mode='write'){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->update($tabname,$data,$condition,$dbconn);	
	}

	/** 刪除記錄
	* @param String $tabname	表名
	* @param String $condition	條件
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	*/
	function db_del($tabname,$condition,$db='',$mode='write'){
		global $DBOBJ;
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->del($tabname,$condition,$dbconn);	
	}  

	/** 自定義語句
	* @param String $sqlstr 	SQL語句
	* @param Array	$param		參數
	* @param String	$db			DBNAME
	* @param String	$mode		read/write
	*/
	function db_handle($sqlstr,$param=array(),$db='',$mode=''){
		global $DBOBJ;
		if($mode==''){
			$type = explode(' ',$sqlstr);
			$type = strtolower($type[0]);
			switch($type){
				case 'select':  $mode='read'; break;
				case 'insert':  $mode='write'; break;
				case 'update':  $mode='write'; break;
				case 'delete':  $mode='write'; break;
				case 'replace': $mode='write'; break;
				default: $mode='write';
			}
		}
		list($dbm,$dbconn) = $DBOBJ->addConnect($mode,$db);
		return $dbm->handle($sqlstr,$param,$dbconn);
	}
?>